package com.ssplayer.fragment;

import java.util.List;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ssplayer.activity.MusicPlayerActivity;
import com.ssplayer.activity.R;
import com.ssplayer.adapter.MusicAdapter;
import com.ssplayer.adapter.MusicListAdapter;
import com.ssplayer.application.MyApplication;
import com.ssplayer.bean.Music;
import com.ssplayer.bean.MusicAction;
import com.ssplayer.bean.MusicList;
import com.ssplayer.bean.MusicMsg;
import com.ssplayer.database.DBHelper;
import com.ssplayer.utils.MediaUtils;

public class MusicListFragment extends Fragment implements OnClickListener,
		OnItemLongClickListener, OnItemClickListener {

	private Context context;
	private DBHelper dbHelper;
	private TextView backlist, addmusiclist, defaultlist, musiclist_id;
	private ListView musiclist_list;
	private List<MusicList> musiclists;

	public MusicListFragment() {
	}

	public MusicListFragment(Context context) {
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_musiclist, container,
				false);
		dbHelper = new DBHelper(context);
		findAllViews(view);
		showMusicList();
		return view;
	}

	/**
	 * 显示播放列表
	 */
	private void showMusicList() {
		musiclists = MediaUtils.getMusicLists(context);
		MusicListAdapter musicListAdapter = new MusicListAdapter(context,
				musiclists, R.layout.musiclist_item);
		musiclist_list.setAdapter(musicListAdapter);
		musiclist_list.setOnItemLongClickListener(this);

	}

	private void findAllViews(View view) {
		backlist = (TextView) view.findViewById(R.id.back_list);
		addmusiclist = (TextView) view.findViewById(R.id.add_musiclist);
		defaultlist = (TextView) view.findViewById(R.id.default_list);
		musiclist_list = (ListView) view.findViewById(R.id.musiclist_list);
		backlist.setOnClickListener(this);
		addmusiclist.setOnClickListener(this);
		defaultlist.setOnClickListener(this);
		musiclist_list.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View view) {
		Intent intent = new Intent();
		switch (view.getId()) {
		case R.id.back_list:
			intent.setAction(MusicAction.LIST_BACK);
			context.sendBroadcast(intent);
			break;
		case R.id.add_musiclist:
			showListDialog();
			break;
		case R.id.default_list:
			intent.setAction(MusicAction.LIST_DEFAULT);
			context.sendBroadcast(intent);
			break;
		default:
			break;
		}
	}

	/**
	 * 显示播放列表
	 */
	private void showListDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater factory = LayoutInflater.from(context);
		final View dialogview = factory.inflate(R.layout.addlist_dialog, null);
		builder.setTitle("新建列表");
		builder.setView(dialogview);
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				EditText listname = (EditText) dialogview
						.findViewById(R.id.new_list_name);
				try {
					dbHelper.createNewList(listname.getText().toString());
					Toast.makeText(context, "列表创建成功", Toast.LENGTH_SHORT)
							.show();
					// 更新播放列表
					showMusicList();
				} catch (Exception e) {
					Toast.makeText(context, "该列表已存在", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		builder.create().show();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent();
		musiclist_id = (TextView) view.findViewById(R.id.musiclist_id);
		intent.setAction(MusicAction.LIST_OPEN);
		intent.putExtra("listID",
				Integer.parseInt(musiclist_id.getText().toString()));
		context.sendBroadcast(intent);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			final int position, long id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("操作")
				.setItems(new String[] { "重命名", "删除列表" },
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								switch (which) {
								case 0:
									renameList(position);
									break;
								case 1:
									deleteList(position);
									break;

								default:
									break;
								}
							}

						})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				}).create().show();
		return false;
	}

	/**
	 * 重命名列表
	 * 
	 * @param position
	 */
	private void renameList(final int position) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		LayoutInflater factory = LayoutInflater.from(context);
		final View dialogview = factory.inflate(R.layout.addlist_dialog, null);
		builder.setTitle("重命名");
		builder.setView(dialogview);
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				EditText listname = (EditText) dialogview
						.findViewById(R.id.new_list_name);
				try {
					dbHelper.renameList(musiclists.get(position).getId(),
							listname.getText().toString());
					Toast.makeText(context, "修改成功", Toast.LENGTH_SHORT).show();
					// 更新播放列表
					showMusicList();
				} catch (Exception e) {
					Toast.makeText(context, "修改失败", Toast.LENGTH_SHORT).show();
				}
			}
		});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		builder.create().show();
	}

	/**
	 * 删除列表
	 * 
	 * @param position
	 */
	private void deleteList(final int position) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示");
		builder.setMessage("确定删除该列表？");
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dbHelper.deleteList((Integer.parseInt(String.valueOf(musiclists
						.get(position).getId()))));
				// 更新播放列表
				showMusicList();
			}
		});
		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		builder.create().show();
	}

}
